package main;


import java.io.File;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;

import javax.swing.table.DefaultTableModel;

import objects.ClientImpl;
import tinyTorrent.TinyTorrent;
import exceptions.FileIsNotDirectory;

public class DownloadMain {

	/**
	 * @param args
	 * @throws NotBoundException
	 * @throws FileIsNotDirectory
	 * @throws MalformedURLException
	 * @throws RemoteException
	 */
	public static void main(final String[] args) throws RemoteException, MalformedURLException, FileIsNotDirectory, NotBoundException {

		DownloadMain.download(
	        new File("./torrent/client1/Battle.mp3.tinytorrent"),
	        new ClientImpl(
                new File("./torrent/client4"),
                "rmi://localhost:1099/Tracker"
            ),
            null
        );

	}



	/**
     * Constructor
     */
    private DownloadMain() {
        super();
    }

    public static boolean download(
        final File fileTinyTorrent,
        final ClientImpl client,
        final DefaultTableModel newModel
    ){

        boolean result = false;
		try {
			deleteChunkTrop();

			final Date dStartDate = new Date();
			final Date dEndDate = new Date();
			final Date dStartDate1 = new Date();
			final Date dEndDate1 = new Date();

			final TinyTorrent t = TinyTorrent.read(fileTinyTorrent);
			final ClientImpl l4 = client; //ClientImpl(new File("./torrent/client4"), "rmi://localhost:1099/Tracker");
			System.out.println("Start: " +l4+" at"+ dStartDate.getTime());
			System.out.println("Ratio of client before downloading : "+l4.getRatio());
			System.out.println("flow split : "+l4.getUp());
	//		Calendar dateDepart = Calendar.getInstance();
	//		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM hh:mm:ss" );
	//		System.out.println("Start download "+ sdf.format(dateDepart));
			final File file = l4.download(t, newModel);

			if (file.exists()) {
			    result = true;
			}

			System.out.println("Finish: " +l4+" at"+ dEndDate.getTime());
	//		System.out.println("End of download of "+l4+" = "+sdf.format(dateDepart) );
			System.out.println("Ratio of client after downloading : "+l4.getRatio());
			System.out.println("flow downloaded : "+l4.getDown());

//			ClientImpl l5 = new ClientImpl(new File("./torrent/client5"), "rmi://localhost:1099/Tracker");
//			System.out.println("Start: " +l5+" at"+ dStartDate1.getTime());
//			//		System.out.println("End of download of "+l5+" = "+sdf.format(dateDepart) );
//			System.out.println("Ratio of client before downloading : "+l5.getRatio());
//			System.out.println("flow split : "+l5.getUp());
//			l5.download(t);
//			System.out.println("Finish: " +l5+" at"+ dEndDate.getTime());
//			System.out.println("Ratio of client after downloading : "+l5.getRatio());
//			Sysem.out.println("flow downloaded : "+l5.getDown());
//
//			t = TinyTorrent.read(new File("./torrent/client1/Battle.mp3.tinytorrent"));
//
//			ClientImpl l6 = new ClientImpl(new File("./torrent/client6"), "rmi://localhost:1099/Tracker");
//	//		System.out.println("End of download of "+l6+" = "+sdf.format(dateDepart) );
//			System.out.println("Ratio of client before downloading : "+l6.getRatio());
//			System.out.println("flow split : "+l6.getUp());
//			l6.download(t);
//			System.out.println("Ratio of client after downloading : "+l6.getRatio());
//			System.out.println("flow downloaded : "+l6.getDown());
//	//		System.out.println("End of download "+l6+"   "+sdf.format(dateDepart) );
		}
		catch (final RemoteException e)		{e.printStackTrace();}

		return result;
	}

	private static void deleteChunkTrop(){
		new File("./torrent/client1/Glorious.mp3.chunk0").delete();
		new File("./torrent/client1/Glorious.mp3.chunk1").delete();
		new File("./torrent/client1/Glorious.mp3.chunk2").delete();
		new File("./torrent/client1/Glorious.mp3.chunk4").delete();
		new File("./torrent/client2/Glorious.mp3.chunk0").delete();
		new File("./torrent/client2/Glorious.mp3.chunk2").delete();
		new File("./torrent/client2/Glorious.mp3.chunk3").delete();
		new File("./torrent/client2/Glorious.mp3.chunk4").delete();
		new File("./torrent/client3/Glorious.mp3.chunk1").delete();

		new File("./torrent/client1/Battle.mp3.chunk0").delete();
		new File("./torrent/client1/Battle.mp3.chunk3").delete();
		new File("./torrent/client2/Battle.mp3.chunk1").delete();
		new File("./torrent/client2/Battle.mp3.chunk2").delete();
		new File("./torrent/client3/Battle.mp3.chunk0").delete();
		new File("./torrent/client3/Battle.mp3.chunk2").delete();
		new File("./torrent/client3/Battle.mp3.chunk3").delete();

	}

}
